﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PID.Demo.Controls
{
    public unsafe partial class ConfigControl : UserControl
    {
        public readonly PIDController _pid;
        public ConfigControl()
        {
            InitializeComponent();
            _pid = new PIDController((float)P, (float)I, (float)D);
        }
        public string Title
        {
            get => this.groupBox1.Text;
            set => this.groupBox1.Text = value;
        }
        public string LineText
        {
            get => this.line1.Text;
            set => this.line1.Text = value;
        }
        public int LineSize
        {
            get => this.line1.LineSize;
            set => this.line1.LineSize = value;
        }
        public Color LineColor
        {
            get => this.line1.LineColor; set
            {
                this.line1.LineColor = value; this.Invalidate();
            }
        }
        public ScottPlot.Color ToScottPlotColor
        {
            get => new ScottPlot.Color(LineColor.R, LineColor.G, LineColor.B, LineColor.A);
        }
        public float InitialValue { get; set; } = 0f;
        public decimal P
        {
            get => numericUpDown3.Value;
            set => numericUpDown3.Value = value;
        }
        public decimal I
        {
            get => numericUpDown4.Value; set => numericUpDown4.Value = value;
        }
        public decimal D
        {
            get => numericUpDown5.Value; set => numericUpDown5.Value = value;
        }
        public int MaxCount { get; set; } = 100;
        public float TargetValue { get; set; }
        public bool IsUse { get; set; } = true;

        public event Action<ConfigControl> ConfigControlRemove;
        private void tsmiRemove_Click(object sender, EventArgs e)
        {
            if (ConfigControlRemove != null)
                ConfigControlRemove(this);
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            using (Pen p = new Pen(LineColor))
            {
                e.Graphics.DrawRectangle(p, 0, 0, this.Width - 1, this.Height - 1);
            }
        }
        public void GetYs(out double[] y)
        {
            y = new double[MaxCount];
            _pid.P = (float)P;
            _pid.I = (float)I;
            _pid.D = (float)D;
            _pid.InitDefault();
            fixed (double* yy = y)
            {
                double lastValue = InitialValue;
                for (int i = 0; i < MaxCount; i++)
                {
                    lastValue = _pid.Calculate(TargetValue, lastValue);
                    *(yy + i) = lastValue;
                }
            }
        }

        private void tsmiSetting_Click(object sender, EventArgs e)
        {
            using (FrmConfig fc = new FrmConfig(this.Title, this))
            {
                fc.ShowDialog();
            }
        }

        private void tsmiSettingColor_Click(object sender, EventArgs e)
        {
            using (ColorDialog colorDia = new ColorDialog())
            {
                if (colorDia.ShowDialog() == DialogResult.OK)
                {
                    //获取所选择的颜色
                    Color colorChoosed = colorDia.Color;
                    LineColor = colorChoosed;
                }
            }
        }

        private void tsmiStopUse_Click(object sender, EventArgs e)
        {
            this.IsUse = !this.IsUse;
            tsmiStopUse.Text = this.IsUse ? "禁用" : "启用";
        }
    }
}
